package Web;

import Bean.Cart;
import Bean.Order;
import Bean.OrderItem;
import Bean.User;
import service.OrderService;
import service.OrderServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();

    //生成订单
    protected void createOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cart cart = (Cart) request.getSession().getAttribute("cart");
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            request.getRequestDispatcher("/pages/user/login.jsp").forward(request, response);
            return;
        }
        //生成订单，返回订单号
        String order_id = orderService.createOrder(cart, user.getId());
        //将订单号保存在session域中
        request.getSession().setAttribute("order_id", order_id);
        //重定向到/pages/cart/checkout.jsp
//        request.getRequestDispatcher("/pages/cart/checkout.jsp").forward(request,response);
        response.sendRedirect(request.getContextPath() + "/pages/cart/checkout.jsp");
    }

    //查看所有订单
    protected void showAllOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Order> allOrders = orderService.showAllOrder();
        request.setAttribute("allOrders", allOrders);
        //请求转发至pages/manager/order_manager.jsp
        request.getRequestDispatcher("/pages/manager/order_manager.jsp").forward(request, response);
    }

    //发货
    protected void sendOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String order_id = request.getParameter("order_id");
        orderService.sendOrder(order_id);
        //重定向
        response.sendRedirect(request.getContextPath() + "/orderServlet?action=showAllOrder");
    }

    //查看订单详情
    protected void showOrderDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String order_id = request.getParameter("order_id");
        List<OrderItem> orderItems = orderService.showOrderDetail(order_id);
        request.setAttribute("orderItems",orderItems);
        //请求转发
        request.getRequestDispatcher("/pages/order/order_details.jsp").forward(request,response);
    }

    //查看我的订单
    protected void showMyOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = (User) request.getSession().getAttribute("user");
        List<Order> myOrders = orderService.showMyOrders(user.getId());
        request.setAttribute("myOrders", myOrders);
        //请求转发至pages/order/order.jsp
        request.getRequestDispatcher("/pages/order/order.jsp").forward(request, response);
    }

    //签收订单 （确认收货）
    protected void receiveOrder(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String order_id = request.getParameter("order_id");
        orderService.receiveOrder(order_id);
        //重定向回我的订单
        response.sendRedirect(request.getContextPath() + "/orderServlet?action=showMyOrder");
    }
}
